package com.ltmall.api.parse;

import com.ltmall.api.comm.Path;
import com.ltmall.api.parse.bean.GoodsCollection;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CollectionParse {

    /**
     * 商品图片的路径
     */
    static String goodsPath = Path.nginxServerBusinessDir + Path.GOODS + File.separator;

    public static List<GoodsCollection> goodsParse(List<?> list) {
        List<GoodsCollection> collectionList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Object[] obj = (Object[]) list.get(i);

            GoodsCollection collection = new GoodsCollection();

            collection.setCollectionID(Integer.parseInt(obj[0].toString()));
            collection.setGoodsID(obj[1].toString());
            collection.setGoodsName(obj[2].toString());
            collection.setGoodsPicture(getOneGoodsPicture((obj[3] == null) ? null : obj[3].toString()));
            collection.setGoodsPrice(Float.parseFloat(obj[4].toString()));
            collection.setGoodsTitle(obj[5].toString());

            collectionList.add(collection);
        }

        return collectionList;
    }

    /**
     * 返回第一张商品图片的
     *
     * @param allPicture 所有图片url（用;分隔）
     */
    public static String getOneGoodsPicture(String allPicture) {
        if (allPicture == null) {
            return "";
        }
        String picArr[] = allPicture.split(";");
        return goodsPath + picArr[0];
    }
}
